package com.antexpress.service;

import com.antexpress.bean.Authority;
import com.antexpress.common.Response;
import com.antexpress.dao.DepartmentDAO;
import com.antexpress.dao.EmployeeDAO;
import com.antexpress.pojo.Department;
import com.antexpress.pojo.DeptDetail;
import com.antexpress.pojo.Employee;
import com.antexpress.pojo.Title;
import com.antexpress.util.DataUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Andy
 */
@Service
public class DepartmentServiceImpl implements DepartmentService{
    @Autowired
    private DepartmentDAO deptDAO;
    @Autowired
    private EmployeeDAO empDAO;
    
    @Override
    public Response getDeptDetailsByIds(List<String> list) {
        Response resp = null;
        List<DeptDetail> deptDetails = new ArrayList<DeptDetail>();     //返回的部门&职位信息列表
        if(list.size()==0){
            List<Department> deptList = deptDAO.getAllDept();
            for(int i=0; i<deptList.size(); i++){
                DeptDetail deptDetail = new DeptDetail();
                Department dept = deptList.get(i);
                deptDetail.setDepartment(dept);
                List<Title> titles = deptDAO.getTitleListByDept(dept.getId());
                deptDetail.setTitles(titles);
                deptDetails.add(deptDetail);
            }
        }else {
            for(int i=0; i<list.size(); i++){
                DeptDetail deptDetail = new DeptDetail();
                deptDetail.setDepartment(deptDAO.getDeptById(list.get(i)));
                List<Title> titles = deptDAO.getTitleListByDept(list.get(i));
                deptDetail.setTitles(titles);
                deptDetails.add(deptDetail);
            }
        }
        resp = Response.createBySuccess(deptDetails);
        return resp;
    }

    @Override
    public Response getAllDept() {
        Response resp = null;
        List<Department> deptList = deptDAO.getAllDept();
        resp = Response.createBySuccess(deptList);
        return resp;
    }

    @Override
    public Response getAllAuthority() {
        List<Authority> auths = new ArrayList<Authority>();
        Authority auth = new Authority(0, "Basic");
        auths.add(auth);
        auth = new Authority(1, "Courier");
        auths.add(auth);
        auth = new Authority(2, "Area Admin");
        auths.add(auth);
        auth = new Authority(3, "Bill Check");
        auths.add(auth);
        auth = new Authority(5, "Normal Dept. Admin");
        auths.add(auth);
        auth = new Authority(6, "Express Dept. Admin");
        auths.add(auth);
        auth = new Authority(7, "HR Dept Admin");
        auths.add(auth);
        auth = new Authority(10, "Supervisor");
        auths.add(auth);
        return Response.createBySuccess(auths);
    }

    @Override
    public Response addOrUpdateDepartment(Department dept) {
        if(dept.getId()==null||dept.getId().equalsIgnoreCase("")){
            dept.setId(DataUtil.getUUID());
            dept.setCreatetime(DataUtil.getTimestamp());
            dept.setUpdatetime(DataUtil.getTimestamp());
        }else {
            Department d = dept;
            dept = deptDAO.getDeptById(d.getId());
            dept.setName(d.getName());
            dept.setComment(d.getComment());
            dept.setUpdatetime(DataUtil.getTimestamp());
        }
        boolean isSuccess = deptDAO.saveOrUpdateDepartment(dept);
        Response resp = null;
        if(isSuccess)
            resp = Response.createBySuccess();
        else
            resp = Response.createByErrorMessage("Failed to add or update the department!");
        return resp;
    }
    
    @Override
    public Response addOrUpdateTitle(Title title) {
        if(title.getId()==null||title.getId().equalsIgnoreCase("")){
            title.setId(DataUtil.getUUID());
            title.setCreatetime(DataUtil.getTimestamp());
            title.setUpdatetime(DataUtil.getTimestamp());
        }else {
            Title t = title;
            title = deptDAO.getTitleById(title.getId());
            title.setName(t.getName());
            title.setDepartment(t.getDepartment());
            title.setAuthority(t.getAuthority());
            title.setComment(t.getComment());
            title.setUpdatetime(DataUtil.getTimestamp());
        }
        boolean isSuccess = deptDAO.saveOrUpdateTitle(title);
        Response resp = null;
        if(isSuccess)
            resp = Response.createBySuccess();
        else
            resp = Response.createByErrorMessage("Failed to add or update the title!");
        return resp;
    }

    @Override
    public Response getAllTitle() {
        Response resp = null;
        List<Title> titleList = deptDAO.getAllTitle();
        resp = Response.createBySuccess(titleList);
        return resp;
    }

    @Override
    public Response getAllDeptAndTitle() {
        Response resp = null;
        List<Department> deptList = deptDAO.getAllDept();
        List<Title> titleList = deptDAO.getAllTitle();
        Map<String,Object> data = new HashMap<String, Object>();
        data.put("dept", deptList);
        data.put("title", titleList);
        resp = Response.createBySuccess(data);
        return resp;
    }

    @Override
    public Title getTitleById(String id) {
        return deptDAO.getTitleById(id);
    }

    @Override
    public Response getTitlesByDeptId(String deptId) {
        Response resp = null;
        List<Title> titles = deptDAO.getTitleListByDept(deptId);
        resp = Response.createBySuccess(titles);
        return resp;
    }

    @Override
    public Response deleteTitleById(String titleId) {
        Response resp = null;
        Title title = deptDAO.getTitleById(titleId);
        if(title!=null){
            List<Employee> emp = empDAO.getEmpsByTitle(title);
            for(Employee e:emp){
                e.setStatus(-10);
                e.setUpdatetime(new Date());
            }
            boolean isDelEmp = empDAO.batchUpdateEmp(emp);
            if(isDelEmp){
                boolean isDelTitle = deptDAO.deleteTitle(title);
                if(isDelTitle){
                    resp = Response.createBySuccessMessage("Delete success!");
                }else{
                    resp = Response.createByErrorMessage("Opps, something goes error!");
                }
            }else{
                resp = Response.createByErrorMessage("Opps, something goes error!");
            }
        }else{
            resp = Response.createByErrorMessage("The title has been deleted!");
        }
        return resp;
    }

    @Override
    public Response deleteDeptById(String deptId) {
        Response resp = null;
        Department dept = deptDAO.getDeptById(deptId);
        if(dept!=null){
            List<Title> titles = deptDAO.getTitleListByDept(deptId);
            for(Title t:titles){
                List<Employee> emp = empDAO.getEmpsByTitle(t);
                for(Employee e:emp){
                    e.setStatus(-10);
                    e.setUpdatetime(new Date());
                }
                boolean isDelEmp = empDAO.batchUpdateEmp(emp);
                if(isDelEmp){
                    boolean isDelTitle = deptDAO.deleteTitle(t);
                    if(isDelTitle){
                        resp = Response.createBySuccessMessage("Delete success!");
                    }else{
                        return Response.createByErrorMessage("Opps, something goes error!");
                    }
                }else{
                    return Response.createByErrorMessage("Opps, something goes error!");
                }
            }
            //delete
            boolean isDeptDel = deptDAO.deleteDept(dept);
            if(isDeptDel){
                resp = Response.createBySuccessMessage("Delete success!");
            }else{
                resp = Response.createByErrorMessage("Delete failed!");
            }
        }else{
            resp = Response.createByErrorMessage("Sorry, the department is not existed!");
        }
        return resp;
    }

    @Override
    public Response batchDeleteTitles(String[] titles) {
        Response resp = null;
        List<Title> ts = new ArrayList<Title>();
        for(String t:titles){
            Title title = deptDAO.getTitleById(t);
            if(title!=null)
                ts.add(title);
        }
        List<Employee> emps = new ArrayList<Employee>();
        if(!ts.isEmpty()){
            emps = empDAO.getEmpsByTitles(ts);
        }
        for(Employee e:emps){
            e.setStatus(-10);
            e.setUpdatetime(new Date());
        }
        boolean isEmpUpdate = empDAO.batchUpdateEmp(emps);
        if(isEmpUpdate){
            if(deptDAO.batchDeleteTitle(ts))
                resp = Response.createBySuccessMessage("Delete success!");
            else
                resp = Response.createByErrorMessage("Delete failed!");
        }else{
            resp = Response.createByErrorMessage("Opps, something goes error!");
        }
        return resp;
    }
    
}
